/**
 * 
 */
package front2;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Scanner;

import front.PM;
import front.User;
import be.db_backend;
import mt.middle;

public class Commands {
	
	static middle m;
	static int userID;
	static Scanner s;
	/**
	 * has no use yet.
	 */
	protected String sRet;
	/**
	 * the command input split in strings separated by " "
	 */
	protected String[] ret;
	/**
	 * command for returning backwards/up
	 */
	public static final String RETURN ="return";
	/**
	 * command for going into a lower level
	 */
	public static final String GO ="go";
	/**
	 * command for modify a content or anything that could be modified
	 */
	public static final String CHANGE ="mod";
	/**
	 * command for adding new content
	 */
	public static final String ADD_CONTENT = "add";
	
	/**
	 * command for loggin out.
	 */
	public static final String LOGOUT = "logout";
	/**
	 * command for returning the help string.
	 */
	public static final String HELP = "help";
	/**
	 * show current, if the console is getting filled with other stuff.
	 */
	public static final String CLEAR = "clear";
	/**
	 * 
	 */
	public static final String DELETE = "delete";
	
	/**
	 * constructor
	 */
	public Commands(){
		m = new middle(new db_backend());
	}
	public void help(){
		System.out.println("*************************");
		System.out.println("Commands avable: ");
		System.out.println("return - go one step up");
		System.out.println("logout - return to the start menu");
		System.out.println("go [number of sub] - go to the sub part with the number");
		System.out.println("mod");
		System.out.println("add");
		System.out.println("clear");
		System.out.println("users - print all the users");
		System.out.println("pm - send a PM");
		System.out.println("inbox - print your inbox");
		System.out.println("outbox - print your outbox");
		System.out.println("help");
		System.out.println("*************************");
	}
	
	public static String readCommand(String out){
		System.out.println(out);
		return s.nextLine();
	}
	
	/**
	 * returns true if the command can be handled internally in this method.
	 * @return
	 */
	public boolean readCommand(){
		ret =  s.nextLine().split(" ");
		if(ret[0].trim().equals(HELP)){
			help();
			return true;
		}
		if(ret[0].trim().equals(CLEAR)){
			clear();
			return true;
		}
		if(ret[0].trim().equals("users")){
			printUsers();
			return true;
		}
		if(ret[0].trim().equals("inbox")){
			inbox();
			return true;
		}
		if(ret[0].trim().equals("outbox")){
			outbox();
			return true;
		}
		if(ret[0].trim().equals("pm")){
			pm();
			return true;
		}
		return false;
	}
	
	
	private void printUsers() {
		if(userID<0){
			System.out.println("you need to login to see all users!");
		}else{
			ArrayList<front.User> usr = m.getAllUsers();
	
			System.out.println("Id Name Email Joined NumberOfPosts");
			for(User u : usr){
				System.out.println(u.getUserID() + " " + u.getName() + " " + u.getEmail() + " " + u.getJoined() + " " + u.getNumberOfPosts() );
			}
		}
	}
	private void pm() {
		if(userID<0){
			System.out.println("you need to login to send pms!");
		}
		else{
			int to = Integer.parseInt(readCommand("To what userId do you want to send a PM?"));
			
			User test = m.getUser(to);
			if(test != null){
				String msg = readCommand("And the message?");
				front.PM pm = new PM(0, userID, to, new Timestamp(System.currentTimeMillis()), msg);
				pm.Create();
			}
			else{
				System.out.println("No user with that ID!");
			}
		}
		
	}
	private void outbox() {
		
		if(userID<0){
			System.out.println("You need to login to see PMs!");
		}else{
			ArrayList<PM> pms = m.getAllPMSFromAUser(userID);
			
			for(PM p : pms){
				System.out.println(p.getDate() + " - " + p.getEmailOfFrom() + "->" + p.getEmailOfTo() + " : " + p.getMessage());
			}
		}
		
	}
	private void inbox() {
		if(userID<0){
			System.out.println("You need to login to see PMs!");
		}else{
		
			ArrayList<PM> pms = m.getAllPMSToAUser(userID);
			
			for(PM p : pms){
				System.out.println(p.getDate() + " - " + p.getEmailOfFrom() + "->" + p.getEmailOfTo() + " : " + p.getMessage());
			}
		}
		
	}
	public boolean isAdd() {
		if(ret[0].trim().equals(ADD_CONTENT))
			return true;
		return false;
	}
	public boolean isLogout() {
		if(ret[0].trim().equals(LOGOUT))
			return true;
		return false;
	}
	
	public boolean isMod() {
		if(ret[0].trim().equals(CHANGE))
			return true;
		return false;
	}
	
	/**
	 * check if the command is return
	 * @return returns true if it is.
	 */
	public boolean isUp(){
		if(ret[0].trim().equals(RETURN))
			return true;
		return false;
	}	
	
	public boolean isDown(){
		if(ret[0].trim().equals(GO))
			return true;
		return false;
	}	
	
	public void clear(){
		for(int i = 0;i<50;i++)
			System.out.println();
	}
}
